package com.twinkle.bank;

/**
 * 银行类
 */
public class Bank {

    private static final Bank bank = new Bank();
    private final Account[] accounts;
    private static final int count = 3;             //只有3个账户

    private Bank() {
        accounts = new Account[10];
        accounts[0] = new SavingAccount("S001", "123456", 1000);
        accounts[1] = new SavingAccount("S002", "123456", 1000);
        accounts[2] = new CreditAccount("C001", "123456", 1000, 500);
    }

    public static Bank getInstance() {
        return bank;
    }

    /**
     * 卡号验证
     *
     * @param cardNo   卡号
     * @param password 密码
     * @return 账户信息
     */
    public Account verify(String cardNo, String password) {
        Account account = null;
        for (int i = 0; i < count; i++) {
            if (cardNo.equals(accounts[i].getCardNo()) && password.equals(accounts[i].getPassword())) {
                account = accounts[i];
                break;
            } else {
                account = null;
            }
        }
        return account;
    }

    /**
     * 存钱
     *
     * @param account 账户信息
     * @param money   待存金额
     */
    public void saveMoney(Account account, int money) {
        account.save(money);
    }

    /**
     * 取钱
     *
     * @param account 账户信息
     * @param money   待取金额
     * @throws NotEnoughMoneyException 异常：账户没钱
     */
    public void withdrawMoney(Account account, int money) throws NotEnoughMoneyException {
        account.withdraw(money);
    }

    public int showBalance(Account account) {
        return account.getBalance();
    }

    public void loan(IRepay account, int money) {
        account.loan(money);
    }
}
